package com.fyzk.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.lottery.basketball.JCLQPayActivity;
import com.fyzk.activity.lottery.jc.football.JZBetContentUtil;
import com.fyzk.activity.lottery.jc.football.JZBetNumUtil;
import com.fyzk.activity.lottery.jc.football.guoguan.JC_GuoGuanDialog;
import com.fyzk.interfaces.OnBasketPayTeamDeleteListener;
import com.tyjc.lottery.R;
import com.fyzk.model.JCLQMatchData;
import com.fyzk.net.PublicMethod;

import java.util.ArrayList;
import java.util.List;

public class JCLQHGPayAdapter extends JCLQZhiFuBaseAdapter {

    private static final String TAG = "JC_ZhiFuAdapter";

    private boolean isdanguan;
    private List<JCLQMatchData> infors;
    private LayoutInflater mInflater;
    private Handler handler;
    private int zhuShu;
    private OnBasketPayTeamDeleteListener listener;


    public JCLQHGPayAdapter(Context context, boolean type,
                            List<JCLQMatchData> infors, Handler handler, OnBasketPayTeamDeleteListener listener) {
        this.isdanguan = type;
        this.infors = getInfor(infors);
        mInflater = LayoutInflater.from(context);
        this.handler = handler;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return infors.size();
    }

    @Override
    public Object getItem(int position) {
        if (position >= infors.size()) {
            return null;
        }
        return infors.get(position);
    }

    @Override
    public long getItemId(int position) {
        if (position >= infors.size()) {
            return 0;
        }
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.jclq_pay_item_sfc, null);
            holder = new ViewHolder();
            holder.zhu = convertView.findViewById(R.id.jclq_zhifu_list_item_zhu);
            holder.delete = convertView.findViewById(R.id.jclq_zhifu_list_item_delete);
            holder.sheng1 = convertView.findViewById(R.id.jclq_zhifu_list_item_tv_sheng1);
            holder.fu1 = convertView.findViewById(R.id.jclq_zhifu_list_item_tv_fu1);
            holder.result = convertView.findViewById(R.id.jclq_zhifu_list_item_tv_result);
            holder.week_num = convertView.findViewById(R.id.week_num);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        JCLQMatchData infor = infors.get(position);
        holder.week_num.setText(infor.getOfficialWeekDay() + PublicMethod.getTeamNum(infor.getMatchNum()));
        holder.zhu.setVisibility(View.VISIBLE);
        holder.sheng1.setText(infor.getHomeTeam());
        holder.fu1.setText(infor.getAwayTeam());
        holder.result.setText(infor.getSFCResult());
        holder.delete.setTag(infor);
        holder.delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                JCLQMatchData infor = (JCLQMatchData) v.getTag();
                delQueue(infor);
                if (listener != null) {
                    listener.onDelete(infor);
                }
            }
        });
        jiSuanZhuShu();
        return convertView;
    }


    private void delQueue(JCLQMatchData infor) {
        for (JCLQMatchData.SPInfo pl : infor.plInfos) {
            pl.isSel = false;
        }
        infor.setSfcNum(0);
        infor.clickNum = 0;
        infors.remove(infor);
        notifyDataSetChanged();
    }

    /**
     * 获得支付list数据
     *
     * @param infors
     * @return
     */
    private List<JCLQMatchData> getInfor(List<JCLQMatchData> infors) {
//		List<JCLQMatchData> list = new ArrayList<JCLQMatchData>();
//		for (int i = 0; i < jcInfors.size(); i++) {
//			JCLQMatchData info = jcInfors.get(i);
//			if (info.getOnClickNum()>0) {
//				list.add(info);
//			}
//		}
        return infors;
    }


    /**
     * 计算注数（待修改）
     */
    public void jiSuanZhuShu() {

        List<Integer> clicknums = new ArrayList<Integer>();
        List<Boolean> dannums = new ArrayList<Boolean>();

        zhuShu = 0;
        int changCi = 0;
        int wanFa = 1;
        boolean isDanShi = true;
        changCi = infors.size();
        for (int i = 0; i < infors.size(); i++) {
            JCLQMatchData info = infors.get(i);
            clicknums.add(info.clickNum);

            dannums.add(false);// 暂不支持胆拖
        }
        List<Integer> chuans = new ArrayList<Integer>();
//		JC_GuoGuanDialog.setResult1(changCi);
        if (isdanguan) {
            for (int i = 0; i < clicknums.size(); i++) {
                zhuShu += clicknums.get(i);
            }
        } else {
            String chuan = JC_GuoGuanDialog.result;

            if (chuan.contains(",") || chuan.endsWith("串1")){
                if (chuan.contains("串")) {
                    String[] chuan_array = chuan.split(",");
                    for (int i = 0; i < chuan_array.length; i++) {
                        String chuan_i = chuan_array[i];
                        chuans.add(Integer.valueOf(chuan_i.charAt(0) + ""));
                    }
                    for (Integer i : chuans) {
                        zhuShu += JZBetNumUtil.getZhushu(i, clicknums, dannums);
                    }
                }
            } else if (chuan.contains("串")) {
                String[] split = chuan.split("串");
                int teamNum = Integer.valueOf(split[0]);
                int select = Integer.valueOf(split[1]);
//                zhuShu = JZBetNumUtil.getDuoZhushu(teamNum, select, dannums, clicknums);
//                // L场比赛选择M关的可能性为groupNum种组合情况
//                int groupNum = JZBetNumUtil.getZhushu(teamNum, clicknums, dannums);
                String betcode = JZBetContentUtil.getContentLQHG(isdanguan, chuan);
                zhuShu = JZBetNumUtil.caculateZhushu(chuan, betcode, teamNum, isdanguan, dannums);
            }
        }

        Message msg = handler.obtainMessage();
        msg.obj = zhuShu;
        msg.arg1 = changCi;
        msg.arg2 = wanFa;
        msg.what = JCLQPayActivity.ZF_WHAT1;
        handler.sendMessage(msg);

    }


    public List<JCLQMatchData> getInfors() {
        return infors;
    }

    public void setJLInfors(List<JCLQMatchData> infors, boolean isDanguan) {
        this.isdanguan = isDanguan;
        this.infors = getInfor(infors);
        this.notifyDataSetChanged();
    }

    class ViewHolder {
        TextView result;
        TextView week_num;
        LinearLayout zhu;
        LinearLayout delete;
        TextView sheng1;
        TextView fu1;

    }

}
